﻿/*
创建人：zhangrj
日期：2022-07-28
描述：工步派工窗体页面js
*/

var page = {
    pagetotal: 0,
    pageSize: 10,
    pageNumber: 1,
    parentID: "", //父窗体ID
    url: getRootPath() + "/Ashx/ContainerStepInfoDispatchHandler.ashx", //后台处理逻辑url
    postFormData: { //保存提交时的数据
        action: "SaveDispatchData"
    },
    init: function () {
        page.parentID = getParam("parentDiv");
        page.bind();
        page.initPageData();
    },
    //初始化页面事件
    bind: function () {
        //批次列表
        $("#gdContainerTable").datagrid({
            columns: [[
		         { field: 'CONTAINERNAME', title: '批次号', width: 180 },
                 { field: 'PRODUCTNAME', title: '零件号', width: 128 },
                 { field: 'PRODUCTDESCRIPTION', title: '零件名称', width: 180 },
                 { field: 'PLANQTY', title: '计划数', width: 80 },
                 { field: 'WIPQTY', title: '在制数', width: 80 },
                 { field: 'WORKFLOWSTEPNAME', title: '当前工序', width: 180 },
                 { field: 'CONTAINERTYPENAME', title: '批次类型', width: 140 }
                ]]
        });
        //工序列表
        $("#gdSpecTable").datagrid({
            columns: [[
               { field: 'ck', title: '选择', checkbox: true, width: 30 },
		       { field: 'WORKFLOWSTEPNAME', title: '工序', width: 250 },
               { field: 'WORKFLOWSTEPID', hidden: true }
                ]],
            onSelect: function (rowIndex, rowData) {
                page.bindStepInfoList(rowData.WORKFLOWSTEPID);
            }
        });
        //工步列表
        $("#gdStepInfo").datagrid({
            columns: [[
               { field: 'ck', title: '选择', checkbox: true, width: 30 },
		       { field: 'STEPINFODESC', title: '工步', width: 240 },
               { field: 'CANDISQTY', title: '可派数', width: 80 },
               { field: 'STEPINFOID', hidden: true }
                ]],
            onSelect: function (rowIndex, rowData) {
                $('#txtQty').textbox("setValue", rowData.CANDISQTY);
            },
            onCheckAll: function (rows) {
                var qty = parseInt(rows[0].CANDISQTY, 10);
                for (var i = 0; i < rows.length; i++) {
                    if (qty > parseInt(rows[i].CANDISQTY, 10)) {
                        qty = parseInt(rows[i].CANDISQTY, 10);
                    }
                }
                $('#txtQty').textbox("setValue", qty);
            }
        });
        //零件序号
        $("#gdProductTable").datagrid({
            columns: [[
		         { field: 'CONTAINERNAME', title: '零件序号', width: 250 },
                { field: 'CONTAINERID', title: '', width: 200, hidden: true }
                ]]
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.bindDispatchTableData();
            }
        });
        //派工
        $('#btnStart').on("click", function () {
            page.postData("1");
        });
        //派工并关闭
        $('#btnStartAndClosed').on("click", function () {
            page.postData("2");
        });
        //关闭
        $('#btnClose').on("click", function () {
            parent.window.returnValue = { page: "YPContainerDispatchFormPage.html", isSuccess: true, message: "已关闭窗体" };
            parent.$("#" + page.parentID).dialog("close");
        })
    },
    //初始化页面数据
    initPageData: function () {
        var containers = parent.$("#" + page.parentID).dialog('options').poPupData;
        if (containers.length == 0) {
            displayMessage("缓存过期", false);
            return;
        }
        page.post(page.url, { action: "GetInitData", containers: JSON.stringify(containers) }, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, false);
                return;
            }
            //批次列表
            $("#gdContainerTable").datagrid('loadData', { rows: res.DBDataSet.dtContainerNew });
            //工序列表
            var datas = [];
            datas.push(containers[0]);
            //工序列表
            $("#gdSpecTable").datagrid('loadData', { rows: datas });
            if (containers[0].CONTAINERTYPE === 7) { //如果是单件单卡，则不让其输入数量
                $("#txtQty").attr('readonly', true);
            }
            $("#gdSpecTable").datagrid('selectRow', 0);
            page.bindDefaultPageData(res.DBDataSet);
        });
    },
    //绑定初始化后台获取的数据
    bindDefaultPageData: function (resData) {
        //        //工序列表
        //        $("#gdSpecTable").datagrid('loadData', { rows: resData.dtWorkStep });
        //        if (resData.dtWorkStep.length > 0) {
        //            $("#txtQty").textbox('setValue', resData.dtWorkStep[0].CanDispatchQty); //默认绑定第一个工序的可派数
        //        }
        //子工序列表
        $("#gdProductTable").datagrid('loadData', { rows: resData.childDt });
        //优先级
        $("#selPriorityCode").combobox({
            valueField: 'PRIORITYCODE',
            textField: 'PRIORITYCODENAME',
            data: resData.natdt, //下拉数据
            value: resData.natdt[0].PRIORITYCODE//默认选中第一个
        });
        //班组数据
        $("#selTeam").combobox({
            valueField: 'TEAMID',
            textField: 'TEAMNAME',
            panelHeight: 200,
            data: resData.dtTeam, //下拉数据
            value: resData.dtTeam[0].TEAMID, //默认选中第一个
            onSelect: function (record) { //下拉时动态获取操作工
                $("#selResource").combobox("clear");
                page.post(page.url, { action: "GetOperator", teamID: record.TEAMID }, true, function (res) {
                    $('#selOperator').combogrid({
                        multiple: true, //设置允许多选
                        panelWidth: 150,
                        panelHeight: 200,
                        idField: 'EMPLOYEEID',
                        textField: 'FULLNAME',
                        checkOnSelect: true,
                        columns: [[
                                { field: 'ck', title: '', width: 50, checkbox: true },
                                { field: 'EMPLOYEEID', title: '', width: 60, hidden: true },
                                { field: 'FULLNAME', title: '姓名', width: 110 }
                            ]],
                        data: res.DBTable,
                        onHidePanel: function () {
                            $("#selResource").combobox("clear");
                            var rows = $('#selOperator').combogrid('grid').datagrid('getChecked');
                            var stremployeeid = "";
                            var resourceData = []; //add:Wangjh 20220302
                            for (var i = 0; i < rows.length; i++) {
                                mergerNoRepeat(resourceData, JSON.parse(rows[i].RESOURCES));
                            }
                            $("#selResource").combobox({ //绑定设备
                                valueField: 'RESOURCEID',
                                textField: 'RESNAME',
                                panelHeight: 160,
                                data: resourceData //下拉数据
                            });
                        }
                    });
                });
            }
        });
        addClearBtnToDatebox(); //添加日期清除按钮
    },
    //动态绑定工步列表
    bindStepInfoList: function (workflowstepId) {
        var dtContainers = $("#gdContainerTable").datagrid('getRows');
        if (dtContainers.length == 0) {
            displayMessage("未有批次信息", false);
            return;
        }
        page.post(page.url, { action: "GetStepInfoList", workflowstepId: workflowstepId, containerId: dtContainers[0].CONTAINERID }, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            $("#gdStepInfo").datagrid('loadData', { rows: res.DBTable });
        });
    },
    //提交数据
    postData: function (btype) {
        clearMessage(); //清空提示消息
        $('#btnStart').linkbutton('disable'); //禁用按钮
        $('#btnStartAndClosed').linkbutton('disable'); //禁用按钮
        var stepInfoRows = $("#gdStepInfo").datagrid('getChecked');
        if (stepInfoRows.length == 0) {
            displayMessage2("请选择工步", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }
        page.postFormData.teamId = $("#selTeam").combobox('getValue'); //班组ID
        if (isNullOrWhiteSpaceStr(page.postFormData.teamId)) {
            displayMessage2("请选择班组", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }
        var operaRows = $('#selOperator').combogrid('grid').datagrid('getChecked'); //操作工  
        if (operaRows.length == 0) {
            displayMessage2("请选择操作工", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }

        page.postFormData.priorityCode = $("#selPriorityCode").combobox('getValue'); //优先级
        page.postFormData.resource = $("#selResource").combobox('getValue'); //设备ID
        if (isNullOrWhiteSpaceStr(page.postFormData.resource)) {
            displayMessage2("请选择设备", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }
        var resourceName = $("#selResource").combobox('getText'); //设备名
        if (resourceName === page.postFormData.resource) {
            displayMessage2("未找到该设备", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }
        page.postFormData.dispatchQty = $("#txtQty").textbox('getValue'); //数量
        if (isNullOrWhiteSpaceStr(page.postFormData.dispatchQty)) {
            displayMessage2("请填写数量", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }

        page.postFormData.startDate = $("#txtStartDate").datebox('getValue'); //开始日期
        page.postFormData.endDate = $("#txtEndDate").datebox('getValue'); //结束日期

        if (isNullOrWhiteSpaceStr(page.postFormData.startDate) || isNullOrWhiteSpaceStr(page.postFormData.endDate)) {
            displayMessage2("请选择开始日期或结束日期", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }
        //批次列表
        var dtContainers = $("#gdContainerTable").datagrid('getRows');
        if (dtContainers.length == 0) {
            displayMessage2("未有批次信息", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }
        page.postFormData.dtContainers = JSON.stringify(dtContainers); //批次列表
        page.postFormData.stepInfoDatas = JSON.stringify(stepInfoRows); //工步列表
        page.postFormData.operatorDatas = JSON.stringify(operaRows); //操作工列表

        page.post(page.url, page.postFormData, true, function (res) {
            if (res.Result) {
                if (btype == "1") { //派工
                    page.clearFillData(); //清空填写数据
                    page.initPageData(); //初始化页面
                }
                if (btype == "2") { //派工并关闭
                    parent.window.returnValue = { isSuccess: true, message: res.Message };
                    parent.$("#" + page.parentID).dialog("close");
                }
            }
            displayMessage2(res.Message, res.Result, ["btnStart", "btnStartAndClosed"]);
        });
    },
    //清空页面填写数据
    clearFillData: function () {
        $("#selTeam").combobox('clear');
        $("#selResource").combobox('clear');
        $("#selOperator").combogrid('clear');
        $("#txtQty").textbox('setValue', "");
        $("#txtStartDate").combo('setText', '');
        $("#txtEndDate").combo('setText', '');
        clearEasyuiDataGrid("gdStepInfo"); //清工步列表
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
};
page.init();